package com.kaikeba.controller;

import com.kaikeba.bean.BootStrapTableCourier;
import com.kaikeba.bean.Courier;
import com.kaikeba.bean.Message;
import com.kaikeba.bean.PageData;
import com.kaikeba.exception.DuplicateCodeException;
import com.kaikeba.mvc.ResponseBody;
import com.kaikeba.service.CouriorService;
import com.kaikeba.service.ExpressService;
import com.kaikeba.util.DateFormatUtil;
import com.kaikeba.util.JSONUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CourierController {
    @ResponseBody("/courier/console.do")
    public String console(HttpServletRequest request, HttpServletResponse response){
        List<Map<String, Integer>> console = CouriorService.console();
        Message msg = new Message();
        if(console.size() == 0){
            msg.setStatus(-1);
        }else{
            msg.setStatus(0);
        }
        msg.setData(console);
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/courier/list.do")
    public String list(HttpServletRequest request, HttpServletResponse response){
        int offset = Integer.parseInt(request.getParameter("offset"));
        int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
        List<Courier> list = CouriorService.findAll(true, offset, pageNumber);
        List<BootStrapTableCourier> list2 = new ArrayList<>();
        for(Courier courier:list){
            BootStrapTableCourier bstCourier = new BootStrapTableCourier();
            bstCourier.setNumber(courier.getNumber());
            bstCourier.setName(courier.getName());
            bstCourier.setPhoneNumber(courier.getPhoneNumber());
            bstCourier.setIdCardNum(courier.getIdCardNum()==null?"无":"******************");
            bstCourier.setPassword(courier.getPassword()==null?"未设置密码":courier.getPassword());
            bstCourier.setSendExpressNum(courier.getSendExpressNum());
            String registDate = DateFormatUtil.format(courier.getRegistTime());
            bstCourier.setRegistTime(registDate==null?"无记录":registDate);
            String lastLoginDate = DateFormatUtil.format(courier.getLastLoginTime());
            bstCourier.setLastLoginTime(lastLoginDate==null?"无记录":lastLoginDate);
            list2.add(bstCourier);
        }

        // 将查询出来的集合封装为bootstap-table识别的格式
        PageData pageData = new PageData();
        pageData.setRows(list2);
        //需要查询出快递员的总数
        List<Map<String, Integer>> console = CouriorService.console();
        int amount = Integer.parseInt(String.valueOf(console.get(0).get("total_number")));

        pageData.setTotal(amount);
        String json = JSONUtil.toJSON(pageData);
        return json;
    }

    @ResponseBody("/courier/insert.do")
    public String insert(HttpServletRequest request,HttpServletResponse response){
        //获取参数
        String name = request.getParameter("name");
        String phoneNumber = request.getParameter("phoneNumber");
        String IdCard = request.getParameter("IdCard");
        String passwd = request.getParameter("passwd");
        Courier courier = new Courier(name,phoneNumber,IdCard,passwd);
        // 处理请求
        boolean flag = CouriorService.insert(courier);
        // 编写前端看得懂的信息返回
        Message msg = new Message();
        if(flag){
            msg.setStatus(0);
            msg.setResult("快递员信息添加成功");
        }else{
            msg.setStatus(-1);
            msg.setResult("快递员信息添加失败");
        }
        // 再将 msg转成JSON格式
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/courier/findByNumber.do")
    public String findCourierByNumber(HttpServletRequest request,HttpServletResponse response){
        //获取参数
        String phoneNumber = request.getParameter("phoneNumber");
        // 调取service方法
        Courier courier = CouriorService.findByNumber(phoneNumber);
        // 返回信息
        Message msg = new Message();
        if(courier.getName()!=null){
            msg.setStatus(0);
            msg.setResult("查询信息成功");
            msg.setData(courier);
        }else{
            msg.setStatus(-1);
            msg.setResult("查询信息失败或信息不存在");
        }
        String json = JSONUtil.toJSON(msg);
        return json;

    }

    @ResponseBody("/courier/update.do")
    public String update(HttpServletRequest request,HttpServletResponse response){
        // 接收参数
        String name = request.getParameter("name");
        String phoneNumber = request.getParameter("phoneNumber");
        String newphoneNumber = request.getParameter("newphoneNumber");
        String IdCard = request.getParameter("IdCard");
        String passwd = request.getParameter("passwd");
        System.out.println("name:"+name+" phNum:"+phoneNumber+" newNum"+newphoneNumber+" idcard:"+IdCard);

        Courier courier = new Courier(name,newphoneNumber,IdCard,passwd);
        System.out.println("接收到的快递员信息:"+courier.toString());
        //处理逻辑
        boolean flag = CouriorService.update(phoneNumber, courier);
        //返回消息
        Message msg = new Message();
        if(flag){
            msg.setStatus(0);
            msg.setResult("更新数据成功");
        }else{
            msg.setStatus(-1);
            msg.setResult("更新数据失败");
        }
        // 转成JSon格式返回给浏览器
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/courier/delete.do")
    public String delete(HttpServletRequest request,HttpServletResponse response){
        //接收参数
        String phoneNumber = request.getParameter("phoneNumber");
        boolean flag = CouriorService.delete(phoneNumber);
        Message msg = new Message();
        if(flag){
            msg.setStatus(0);
            msg.setResult("删除成功");
        }else{
            msg.setStatus(-1);
            msg.setResult("删除失败");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

}
